package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.entity.misc.TmsTask;
import cn.buk.qms.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yfdai
 */
@RestController
public class TaskController extends BaseController {

  @Autowired
  private EnterpriseService enterpriseService;


  @GetMapping("/tasks")
  public CommonDto<TmsTask> searchTasks(HttpServletRequest request,
                                        @ModelAttribute("sc") CommonSearchCriteria sc) {
    List<TmsTask> dataList = enterpriseService.searchTasks(getEnterpriseId(request), sc);

    CommonDto<TmsTask> commonDto = new CommonDto<>();
    commonDto.setDataList(dataList);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 查找每日审核任务
   * @param request
   * @param sc
   * @return
   */
  @GetMapping("/dailyAuditTasks")
  public CommonDto<TmsTask> searchDailyTasks(HttpServletRequest request,
                                        @ModelAttribute("sc") CommonSearchCriteria sc) {
    sc.setType(TmsTask.TMS_TASK_TYPE_DAILY_AUDIT);
    List<TmsTask> dataList = enterpriseService.searchTasks(getEnterpriseId(request), sc);

    CommonDto<TmsTask> commonDto = new CommonDto<>();
    commonDto.setDataList(dataList);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 修改任务状态
   * @param request
   * @return
   */
  @PostMapping("/dailyAuditTask/{id}")
  public JsonResult finishDailyTask(HttpServletRequest request,
                                     @PathVariable("id") final int taskId) {
    return  enterpriseService.finishDailyTask(getEnterpriseId(request), getUsername(request), taskId);
  }

  /**
   * 重新发送日审任务相关文件
   * @param request
   * @param taskId
   * @return
   */
  @GetMapping("/dailyAuditTask/{id}/resend")
  public JsonResult resendDailyTask(HttpServletRequest request,
                                    @PathVariable("id") final int taskId) {
    return enterpriseService.resendDailyTask(getEnterpriseId(request), getUsername(request), taskId);
  }
}
